package com.yeyks.commonReference.service.vip.impl;

import com.yeyks.common.base.Arguments;
import com.yeyks.common.em.vip.VipCommissionBillOperatorTypeEnum;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.service.vip.VipCommissionBillService;
import com.yeyks.commonReference.service.vip.VipCommissionOverviewService;
import com.yeyks.vip.dal.domain.VipCommissionBill;
import com.yeyks.vip.dal.dao.VipCommissionBillMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeyks.vip.dal.domain.VipCommissionOverview;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * <p>
 * vip订单分佣明细表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-08-21
 */
@Service
@Slf4j
public class VipCommissionBillServiceImpl extends ServiceImpl<VipCommissionBillMapper, VipCommissionBill> implements VipCommissionBillService {

    @Autowired
    private VipCommissionOverviewService vipCommissionOverviewService;

    /**
     * vip佣金提现
     *
     * @param accountId 账户id
     * @param amount    金额
     * @param orderNo   订单号
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void desc(Integer accountId, Integer amount, String orderNo) {
        Arguments.intIsNull(amount, "提现金额不能为空");
        Arguments.intEqualZero(amount, "提现金额不能为0");
        Arguments.intLtZero(amount, "提现金额有误");
        //获取vip分佣账户
        VipCommissionOverview vipCommissionOverview = vipCommissionOverviewService.selectByAccountId(accountId);
        if (null == vipCommissionOverview.getRemainAmount()
                || vipCommissionOverview.getRemainAmount() < amount) {
            throw new ServiceException("账户剩余金额不足");
        }
        if (!amount.equals(vipCommissionOverview.getRemainAmount())) {
            throw new ServiceException("必须全部提现");
        }
        Date date = new Date();
        //首先修改账户总览表的数据
        vipCommissionOverview.setTotalOutAmount(vipCommissionOverview.getTotalOutAmount() + amount);
        vipCommissionOverview.setRemainAmount(vipCommissionOverview.getRemainAmount() - amount);
        vipCommissionOverview.setUpdateTime(date);
        if (!vipCommissionOverviewService.updateById(vipCommissionOverview)) {
            throw new ServiceException("修改vip佣金账户表异常");
        }
        //写入账户明细表信息
        insertBill(date, accountId,
                null, null,
                VipCommissionBillOperatorTypeEnum.OUT.value(),
                VipCommissionBillOperatorTypeEnum.OUT.desc(),
                amount, orderNo);
    }

    /**
     * vip订单佣金入账
     *
     * @param accountId 账户id
     * @param amount    金额
     * @param orderNo   订单号
     * @param type      类型 1:卡酒店 2:卡运营商 3:卡服务商 4:卡服务商上级 5:非卡酒店 6:非卡运营商 7:非卡服务商 8:非卡服务商上级 9:卡商
     * @param cardNo    卡号
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void vipOrderAdd(Integer accountId, Integer amount, String orderNo, Integer type, String cardNo) {
        if (null == amount || amount < 0) {
            amount = 0;
        }
        //获取vip分佣账户
        VipCommissionOverview vipCommissionOverview = vipCommissionOverviewService.selectByAccountId(accountId);
        if (null == vipCommissionOverview) {
            log.info("分佣账户id不能为空orderNo = {}", orderNo);
            throw new ServiceException("分佣账户id不能为空");
        }
        Date date = new Date();
        vipCommissionOverview.setTotalInAmount(vipCommissionOverview.getTotalInAmount() + amount);
        vipCommissionOverview.setRemainAmount(vipCommissionOverview.getRemainAmount() + amount);
        vipCommissionOverview.setUpdateTime(date);
        if (!vipCommissionOverviewService.updateById(vipCommissionOverview)) {
            throw new ServiceException("修改vip佣金账户表异常");
        }
        //写入账户明细表信息
        insertBill(date, accountId,
                type, cardNo,
                VipCommissionBillOperatorTypeEnum.IN.value(),
                VipCommissionBillOperatorTypeEnum.IN.desc(),
                amount, orderNo);
    }

    /**
     * 生成明细
     *
     * @param date         创建时间
     * @param accountId    账户id
     * @param type         账户类型
     * @param cardNo       卡号
     * @param operatorType 操作类型
     * @param remark       操作类型备注
     * @param amount       操作金额
     * @param orderNo      订单号
     */
    public void insertBill(Date date, Integer accountId, Integer type, String cardNo, Integer operatorType, String remark, Integer amount, String orderNo) {
        //写入账户明细表信息
        VipCommissionBill newBill = new VipCommissionBill();
        newBill.setCreateTime(date);
        newBill.setAccountId(accountId);
        newBill.setRemark(remark);
        newBill.setCardNo(cardNo);
        newBill.setType(type);
        newBill.setOperatorType(operatorType);
        newBill.setOperatorAmount(amount);
        newBill.setOrderNo(orderNo);
        Arguments.isTrue(baseMapper.insert(newBill) != 1, "插入vip佣金账户明细异常");
    }
}
